import { Reducer } from 'redux';
import { routerRedux } from 'dva/router';
import { Effect } from 'dva';
import { stringify } from 'querystring';

import { getMenusCurent } from '@/services/api';
import { setAuthority, getAuthority } from '@/utils/authority';
import { reloadAuthorized } from '@/utils/Authorized';
import { getPageQuery, getcookie } from '@/utils/utils';
import { message } from 'antd';

export interface StateType {
  status?: 'ok' | 'error';
  type?: string;
  currentAuthority?: 'user' | 'guest' | 'admin';
}

export interface LoginModelType {
  namespace: string;
  state: StateType;
  effects: {
    // login: Effect;
    // getCaptcha: Effect;
    // logout: Effect;
    // tokenLogin: Effect;
    // getUserAuthorityByidCard: Effect;
  };
  reducers: {
    // changeLoginStatus: Reducer<StateType>;
  };
}

const Model: LoginModelType = {
  namespace: 'login',

  state: {
    status: undefined,
  },

  effects: {
    *feachGetMenusCurent({ payload, callback }, { call, put }) {
      console.log(payload, 'payload');
      const response = yield call(getMenusCurent, payload);
      console.log('response', response);
      if (response) {
        const authoMenuList = response;
        sessionStorage.setItem('authoMenuList', JSON.stringify(authoMenuList));
        reloadAuthorized(); //获取一遍权限
        if (callback) {
          callback(response);
        }
      } else {
        return false;
      }
    },
  },

  reducers: {
    // changeLoginStatus(state, { payload }) {
    //   setAuthority(payload.currentAuthority);
    //   return {
    //     ...state,
    //     status: payload.status,
    //     type: payload.type,
    //   };
    // },
  },
};

export default Model;
